<?php
namespace app\admin\controller\basis;

use app\admin\library\Auth;
use app\common\controller\Backend;
use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
use PhpOffice\PhpSpreadsheet\Reader\Csv;
use PhpOffice\PhpSpreadsheet\Reader\Xls;
use PhpOffice\PhpSpreadsheet\Reader\Xlsx;
use think\Db;
use think\exception\PDOException;
use think\Request;

/**
* 基础费用管理
*
* @icon fa fa-circle-o
*/
class Import extends Backend
{
    public $type = 0;
    public $types = [
        [
            'val' => 1,
            'name' => '社团基准表',
            'tpl_url' => '/tpl/社团基准导入模板.xlsx',
            'url' => '/CzTIqcuemD.php/benchmark/clubs',
        ],  [
            'val' => 2,
            'name' => '护学基准表',
            'tpl_url' => '/tpl/护学基准导入模板.xlsx',
            'url' => '/CzTIqcuemD.php/benchmark/nursing',
        ],  [
            'val' => 3,
            'name' => '值班基准表',
            'tpl_url' => '/tpl/值班基准导入模板.xlsx',
            'url' => '/CzTIqcuemD.php/benchmark/duty',
        ],  [
            'val' => 4,
            'name' => '课业辅导基准表',
            'tpl_url' => '/tpl/课业辅导基准导入模板.xlsx',
            'url' => '/CzTIqcuemD.php/benchmark/tutor',
        ]
    ];
    public $error_data = [];
    public $start_num = 0;
    public $success_count = 0;

    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        $type = $this->request->request('type');
        $title = $tpl_url = $url = '';
        foreach ($this->types as $v){
            if($v['val'] == $type){
                $title = $v['name'].'导入';
                $tpl_url = $v['tpl_url'];
                $this->type = $type;
                $url = $v['url'];
            }
        }
        if(!$title){
            $this->error('参数错误');
        }
        $this->assign('title',$title);
        $this->assign('url',$tpl_url);
        $this->assign('type',$type);
        $this->assign('jump_url',$url);
    }

    // 首页
    public function index(){
        return $this->view->fetch();
    }

    // 导入操作
    public function import(){
        $operation = $this->request->post('operation');
        if($operation == 'upload'){
            $this->upload_file();
        }else if($operation == 'schedule'){
            $this->schedule();
        }else if($operation == 'implement'){
            $this->implement();
        }
    }



    // 获取文档里面所有数据
    public function get_file_data(){
        $file = $this->request->request('file');
        if (!$file) {
            $this->error(__('Parameter %s can not be empty', 'file'));
        }
        $filePath = ROOT_PATH . DS . 'public' . DS . $file;
        if (!is_file($filePath)) {
            $this->error(__('No results were found'));
        }
        //实例化reader
        $ext = pathinfo($filePath, PATHINFO_EXTENSION);
        if (!in_array($ext, ['csv', 'xls', 'xlsx'])) {
            $this->error(__('Unknown data format'));
        }
        if ($ext === 'csv') {
            $file = fopen($filePath, 'r');
            $filePath = tempnam(sys_get_temp_dir(), 'import_csv');
            $fp = fopen($filePath, 'w');
            $n = 0;
            while ($line = fgets($file)) {
                $line = rtrim($line, "\n\r\0");
                $encoding = mb_detect_encoding($line, ['utf-8', 'gbk', 'latin1', 'big5']);
                if ($encoding !== 'utf-8') {
                    $line = mb_convert_encoding($line, 'utf-8', $encoding);
                }
                if ($n == 0 || preg_match('/^".*"$/', $line)) {
                    fwrite($fp, $line . "\n");
                } else {
                    fwrite($fp, '"' . str_replace(['"', ','], ['""', '","'], $line) . "\"\n");
                }
                $n++;
            }
            fclose($file) || fclose($fp);

            $reader = new Csv();
        } elseif ($ext === 'xls') {
            $reader = new Xls();
        } else {
            $reader = new Xlsx();
        }
        //导入文件首行类型,默认是注释,如果需要使用字段名称请使用name
        $importHeadType = isset($this->importHeadType) ? $this->importHeadType : 'comment';
        //加载文件
        $insert = [];
        try {
            if (!$PHPExcel = $reader->load($filePath)) {
                $this->error(__('Unknown data format'));
            }
            $currentSheet = $PHPExcel->getSheet(0);  //读取文件中的第一个工作表
            $allColumn = $currentSheet->getHighestDataColumn(); //取得最大的列号
            $allRow = $currentSheet->getHighestRow(); //取得一共有多少行
            $maxColumnNumber = Coordinate::columnIndexFromString($allColumn);


            for ($currentRow = 2; $currentRow <= $allRow; $currentRow++) {
                $values = [];
                for ($currentColumn = 1; $currentColumn <= $maxColumnNumber; $currentColumn++) {
                    $val = $currentSheet->getCellByColumnAndRow($currentColumn, $currentRow)->getValue();
                    $values[] = is_null($val) ? '' : trim($val);
                }
                $insert[] = $values;
            }
        } catch (\Exception $exception) {
            $this->error($exception->getMessage());
        }
        return $insert;
    }

    // 错误信息
    public function add_error($line,$msg){
        $this->error_data[] = [
            'line' => $line + 2 + $this->start_num,
            'msg' => $msg
        ];
    }


    // step 1 上传数据
    public function upload_file(){
        // 取出文件里面所有数据
        $data = $this->get_file_data();
        $start_date = $this->request->post('start_data');
        $end_date = $this->request->post('end_date');
        $record_data = [
            'admin_id' => session('admin.id'),
            'admin_name' => session('admin.nickname'),
            'type' => $this->type,
            'all_count' =>count($data),
            'result_data' =>json_encode($data),
            'file_url' => $this->request->request('file'),
            'status' => 0,
            'start_date' =>$start_date,
            'end_date' =>$end_date,
            'create_time' => current_time()
        ];
        if(empty($data)){ // 没有数据
            $this->error('文档没有数据,导入失败');
        }
        $file_id = Db::table('b_basis_import')->insert($record_data,'',true);
        if(!$file_id){
            $this->error('导入记录失败');
        }else{
            $this->success('提交成功','',$file_id);
        }
    }

    // 进度
    public function schedule(){
        $file_id = $this->request->post('file_id');
        $file_info = Db::table('b_basis_import')->where('id',$file_id)->find();
        $num = 10;
        if(empty($file_info)){
            $this->error('参数错误');
        }
        $data = json_decode($file_info['result_data'],1);
        $this->error_data = $file_info['error_data']?json_decode($file_info['error_data']):[];
        $start_num = $file_info['schedule'];
        $end_num = bcadd($start_num,$num);
        $import_data = [];
        $this->start_num = $start_num;
        foreach ($data as $k=>$v){
            if($k >= $start_num && $k < $end_num){
                $import_data[] = $v;
            }
        }
        if($file_info['type'] == 1){
            $title = $this->clubs_import($file_info,$import_data,$file_id);
        }else if($file_info['type'] == 2){ // 护学
            $title = $this->nursing_import($file_info,$import_data,$file_id);
        }else if($file_info['type'] == 3){ // 值班
            $title = $this->duty_import($file_info,$import_data,$file_id);
        }else if($file_info['type'] == 4){ // 课后辅导
            $title = $this->tutor_import($file_info,$import_data,$file_id);
        }

        $schedule = $end_num / $file_info['all_count'];
        $schedule = round($schedule,4) * 100;

        $success_list = Db::table('b_basis_temporary')->where([
            'file_id' => $file_id,
            'is_del' => 0
        ])->select();
        $success = [];
        foreach ($success_list as $v){
            $success[] = json_decode($v['data'],1);
        }

        $result = [
            'success_count' => count($success_list),
            'error_data' => json_encode($this->error_data),
            'error_count' => count($this->error_data),
            'schedule' => $end_num,
        ];

        if($schedule >= 100){
            $schedule = 100;
            $result['status'] = 2;
        }else{
            $schedule = (int)$schedule;
            $result['status'] = 1;
        }
        Db::table('b_basis_import')->where('id',$file_id)->update($result);
        $result['title'] = $title;
        $result['success_list'] = $success;
        $result['all_count'] = $file_info['all_count'];
        $result['error_data'] = $this->error_data;
        $result['schedule'] = $schedule;
        $this->success('查询成功','',$result);
    }


    // 执行
    public function implement(){
        $file_id = $this->request->post('file_id');
        $file_info = Db::table('b_basis_import')->where('id',$file_id)->find();
        $num = 10;
        if(empty($file_info)){
            $this->error('参数错误',$file_info);
        }

        $list = Db::table('b_basis_temporary')->where('is_del',0)->where('file_id',$file_id)->limit($num)->select();

        if($file_info['type'] == 1){
            $this->clubs_implement($list);
        }else if($file_info['type'] == 2){
            $this->nursing_implement($list);
        }else if($file_info['type'] == 3){
            $this->duty_implement($list);
        }else if($file_info['type'] == 4){
            $this->tutor_implement($list);
        }


        $end_num = Db::table('b_basis_temporary')->where('is_del',1)->where('file_id',$file_id)->count();
        $schedule = $end_num / $file_info['success_count'];
        $schedule = round($schedule,4) * 100;

        if(!Db::table('b_basis_temporary')->where('is_del',0)->where('file_id',$file_id)->count()){
            $schedule = 100;
        }
        $result = [
            'schedule' => $end_num,
        ];

        if($schedule >= 100){
            $schedule = 100;
            $result['status'] = 3;
        }else{
            $schedule = (int)$schedule;
        }
        Db::table('b_basis_import')->where('id',$file_id)->update($result);
        $result['all_count'] = $file_info['all_count'];
        $result['schedule'] = $schedule;
        $this->success('查询成功','',$result);
    }

    // 社团基准
    public function clubs_import($file_info,$data,$file_id){
        $title = [
            '周期开始','周期结束','课程名称','课程类型','兴趣班名','课程年级','类型','老师id','上课老师','老师身份证','上课教室','上课时间','已选人数','放学地点','课时'
        ];

        $insert = [
            'admin_id' => session('admin.id'),
            'type' =>$this->type,
            'title' =>json_encode($title),
            'file_id' => $file_id,
            'create_time' => current_time()
        ];
        foreach ($data as $k=>$v){
            $name = $v[5];
            $card = $v[6];
            // 查询是否存在老师
            $admin_id = Db::table('b_admin')->where([
                'nickname'=>$name,
                'id_card'=>$card,
            ])->value('id');
            if(!$admin_id){
                $this->add_error($k,'未找到老师');
                continue;
            }
            if(!in_array($v[4],['主教','助教'])){
                $this->add_error($k,'类型错误');
                continue;
            }
            $cz = Db::table('b_benchmark_clubs c')
                ->join('b_benchmark_clubs_item i','i.b_id = c.id')
                ->where('c.start_time',$file_info['start_date'])
                ->where('c.end_time',$file_info['end_date'])
                ->where('i.teacher_id',$admin_id)
                ->where('i.class_name',  $v[2])
                ->count();
            if($cz){
                $this->add_error($k,'已存在');
                continue;
            }
            $str = $v[8];
            $hours = 0;
            if (strpos( $str,'一') !== false || strpos( $str,'1') !== false) {
                $hours += time_week_count($file_info['start_date'],$file_info['end_date'],1);
                $week = 1;
            }else if (strpos($str,'二') !== false || strpos( $str,'2') !== false) {
                $hours += time_week_count($file_info['start_date'],$file_info['end_date'],2);
                $week = 2;
            }else if (strpos( $str,'三') !== false || strpos( $str,'3') !== false) {
                $hours += time_week_count($file_info['start_date'],$file_info['end_date'],3);
                $week = 3;
            }else if (strpos($str,'四') !== false || strpos( $str,'4') !== false) {
                $hours += time_week_count($file_info['start_date'],$file_info['end_date'],4);
                $week = 4;
            }else if (strpos( $str,'五') !== false || strpos( $str,'5') !== false) {
                $hours += time_week_count($file_info['start_date'],$file_info['end_date'],5);
                $week = 5;
            }else if (strpos( $str,'六') !== false || strpos( $str,'6') !== false) {
                $hours += time_week_count($file_info['start_date'],$file_info['end_date'],6);
                $week = 6;
            }else if (strpos( $str,'日') !== false || strpos( $str,'天') !== false) {
                $hours += time_week_count($file_info['start_date'],$file_info['end_date'],0);
                $week = 0;
            }else{
                $this->add_error($k,'上课时间错误');
                continue;
            }

            $title = [
                '周期开始','周期结束','课程名称','课程类型','兴趣班名','课程年级','类型','老师id','上课老师','老师身份证','上课教室','上课时间','已选人数','放学地点','课时','周'
            ];
            $insert['data'] = json_encode([
                $file_info['start_date'],
                $file_info['end_date'],
                $v[0],
                $v[1],
                $v[2],
                $v[3],
                $v[4],
                $admin_id,
                $v[5],
                $v[6],
                $v[7],
                $v[8],
                $v[9],
                $v[10],
                $hours,
                $week
            ]);
            if(Db::table('b_basis_temporary')->insert($insert)){
                $this->success_count++;
            }else{
                $this->add_error($k,'临时数据入库失败');
            }
        }
        return $title;
    }


    // 执行导入
    public function clubs_implement($list){
        $b_id = 0;
        foreach ($list as $val){
            $v = json_decode($val['data'],1);
            if(!$b_id){ // 没有基准 创建基准
                $b_id = Db::table('b_benchmark_clubs')
                    ->where('start_time',$v[0])
                    ->where('end_time',$v[1])
                    ->value('id');
                if(!$b_id){
                    $b_id = Db::table('b_benchmark_clubs')
                        ->insert([
                            'admin_id' => session('admin.id'),
                            'create_time'=> date('Y-m-d H:i:s'),
                            'start_time' => $v[0],
                            'end_time' => $v[1]
                        ],'',true);
                }

            }
            if(Db::table('b_benchmark_clubs_item')->where([
                'class_name'=>$v[4],
                'teacher_id'=>$v[7],
                'week'=>$v[15],
                'b_id' => $b_id,
            ])->find()){
                Db::table('b_benchmark_clubs_item')->where([
                    'class_name'=>$v[4],
                    'teacher_id'=>$v[7],
                    'week'=>$v[15],
                    'b_id' => $b_id,
                ])->update([
                    'name'=>$v[2],
                    'type'=>$v[3],
                    'grade'=>$v[5],
                    'classroom'=>$v[10],
                    'time'=>$v[11],
                    'hours'=>$v[14],
                    'people'=>$v[12],
                    'teacher_type' =>$v[6],
                ]);
            }else{
                Db::table('b_benchmark_clubs_item')->insert([
                    'name'=>$v[2],
                    'type'=>$v[3],
                    'class_name'=>$v[4],
                    'grade'=>$v[5],
                    'teacher_id'=>$v[7],
                    'classroom'=>$v[10],
                    'time'=>$v[11],
                    'hours'=>$v[14],
                    'week'=>$v[15],
                    'people'=>$v[12],
                    'b_id' => $b_id,
                    'teacher_type' =>$v[6],
                    'create_time' => date('Y-m-d H:i:s')
                ]);
            }

            Db::table('b_basis_temporary')->where('id',$val['id'])->update([
                'is_del'=>1,
            ]);
        }
    }



    // 护学
    public function nursing_import($file_info,$data,$file_id){
        $title = [
            '周期开始','周期结束','工作任务','区域','时间段','老师id','老师','时间','周期次数','周'
        ];

        $insert = [
            'admin_id' => session('admin.id'),
            'type' =>$this->type,
            'title' =>json_encode($title),
            'file_id' => $file_id,
            'create_time' => current_time()
        ];
        foreach ($data as $k=>$v){
            $name = $v[3];
            $card = $v[4];
            if(!$v[4]){
                $this->add_error($k,'无身份证');
                continue;
            }
            // 查询是否存在老师
            $admin_id = Db::table('b_admin')->where([
                'nickname'=>$name,
                'id_card'=>$card,
            ])->value('id');
            if(!$admin_id){
                $this->add_error($k,'未找到老师');
                continue;
            }
            $str = $v[5];
            $hours = 0;
            if (strpos( $str,'一') !== false || strpos( $str,'1') !== false) {
                $hours += time_week_count($file_info['start_date'],$file_info['end_date'],1);
                $week = 1;
            }else if (strpos($str,'二') !== false || strpos( $str,'2') !== false) {
                $hours += time_week_count($file_info['start_date'],$file_info['end_date'],2);
                $week = 2;
            }else if (strpos( $str,'三') !== false || strpos( $str,'3') !== false) {
                $hours += time_week_count($file_info['start_date'],$file_info['end_date'],3);
                $week = 3;
            }else if (strpos($str,'四') !== false || strpos( $str,'4') !== false) {
                $hours += time_week_count($file_info['start_date'],$file_info['end_date'],4);
                $week = 4;
            }else if (strpos( $str,'五') !== false || strpos( $str,'5') !== false) {
                $hours += time_week_count($file_info['start_date'],$file_info['end_date'],5);
                $week = 5;
            }else if (strpos( $str,'六') !== false || strpos( $str,'6') !== false) {
                $hours += time_week_count($file_info['start_date'],$file_info['end_date'],6);
                $week = 6;
            }else if (strpos( $str,'日') !== false || strpos( $str,'天') !== false) {
                $hours += time_week_count($file_info['start_date'],$file_info['end_date'],0);
                $week = 0;
            }else{
                $this->add_error($k,'时间错误');
                continue;
            }
            $insert['data'] = json_encode([
                $file_info['start_date'],
                $file_info['end_date'],
                $v[0],
                $v[1],
                $v[2],
                $admin_id,
                $v[3],
                $v[5],
                $hours,
                $week
            ]);
            if(Db::table('b_basis_temporary')->insert($insert)){
                $this->success_count++;
            }else{
                $this->add_error($k,'临时数据入库失败');
            }
        }
        return $title;
    }

    public function nursing_implement($list){
        $b_id = 0;
        foreach ($list as $val){
            $v = json_decode($val['data'],1);
            if(!$b_id){ // 没有基准 创建基准
                $b_id = Db::table('b_benchmark_nursing')
                    ->where('start_time',$v[0])
                    ->where('end_time',$v[1])
                    ->value('id');
                if(!$b_id){
                    $b_id = Db::table('b_benchmark_nursing')
                        ->insert([
                            'admin_id' => session('admin.id'),
                            'create_time'=> date('Y-m-d H:i:s'),
                            'start_time' => $v[0],
                            'end_time' => $v[1]
                        ],'',true);
                }

            }
            if(Db::table('b_benchmark_nursing_item')->where([
                'b_id' => $b_id,
                'teacher_id'=>$v[5],
                'week_str'=>$v[7],
            ])->find()){
                Db::table('b_benchmark_nursing_item')->where([
                    'b_id' => $b_id,
                    'teacher_id'=>$v[5],
                    'week_str'=>$v[7],
                ])->update([
                    'name'=>$v[2],
                    'address'=>$v[3],
                    'times'=>$v[4],
                    'hours'=>$v[8],
                ]);
            }else{
                Db::table('b_benchmark_nursing_item')->insert([
                    'name'=>$v[2],
                    'address'=>$v[3],
                    'times'=>$v[4],
                    'teacher_id'=>$v[5],
                    'week_str'=>$v[7],
                    'hours'=>$v[8],
                    'week'=>$v[9],
                    'b_id' => $b_id,
                    'create_time' => date('Y-m-d H:i:s')
                ]);
            }

            Db::table('b_basis_temporary')->where('id',$val['id'])->update([
                'is_del'=>1,
            ]);
        }
    }


    public function duty_import($file_info,$data,$file_id){
        $title = [
            '周期开始','周期结束','日期','老师id','老师','类型','周','课时'
        ];

        $insert = [
            'admin_id' => session('admin.id'),
            'type' =>$this->type,
            'title' =>json_encode($title),
            'file_id' => $file_id,
            'create_time' => current_time()
        ];
        foreach ($data as $k=>$v){
            $name = $v[1];
            $card = $v[2];
            if(!$v[2]){
                $this->add_error($k,'无身份证');
                continue;
            }
            // 查询是否存在老师
            $admin_id = Db::table('b_admin')->where([
                'nickname'=>$name,
                'id_card'=>$card,
            ])->value('id');
            if(!$admin_id){
                $this->add_error($k,'未找到老师');
                continue;
            }
            $date = $this->conversion($v[0]);
            $week = date('N',strtotime($date));
            if($week == 7){
                $week = 0;
            }
            if(!in_array($v[3],['白班','夜班','夜宿班'])){
                $this->add_error($k,'类型只能是白班,夜班,夜宿班');
                continue;
            }
            $hours = time_week_count($file_info['start_date'],$file_info['end_date'],$week);
            $hours = 1;
            $insert['data'] = json_encode([
                $file_info['start_date'],
                $file_info['end_date'],
                $date,
                $admin_id,
                $v[1],
                $v[3],
                $week,
                $hours
            ]);
            if(Db::table('b_basis_temporary')->insert($insert)){
                $this->success_count++;
            }else{
                $this->add_error($k,'临时数据入库失败');
            }
        }
        return $title;
    }



    public function duty_implement($list){
        $b_id = 0;
        foreach ($list as $val){
            $v = json_decode($val['data'],1);
            if(!$b_id){ // 没有基准 创建基准
                $b_id = Db::table('b_benchmark_duty')
                    ->where('start_time',$v[0])
                    ->where('end_time',$v[1])
                    ->value('id');
                if(!$b_id){
                    $b_id = Db::table('b_benchmark_duty')
                        ->insert([
                            'admin_id' => session('admin.id'),
                            'create_time'=> date('Y-m-d H:i:s'),
                            'start_time' => $v[0],
                            'end_time' => $v[1]
                        ],'',true);
                }

            }
            if(Db::table('b_benchmark_duty_item')->where([
                'teacher_id'=>$v[3],
                'date'=>$v[2],
                'b_id' => $b_id,
            ])->find()){
                Db::table('b_benchmark_duty_item')
                    ->where([
                        'teacher_id'=>$v[3],
                        'date'=>$v[2],
                        'b_id' => $b_id,
                    ])->update([
                        'type'=>$v[5],
                        'week'=>$v[6],
                        'hours'=>$v[7]
                    ]);
            }else{
                Db::table('b_benchmark_duty_item')->insert([
                    'date'=>$v[2],
                    'teacher_id'=>$v[3],
                    'type'=>$v[5],
                    'week'=>$v[6],
                    'hours'=>$v[7],
                    'b_id' => $b_id,
                    'create_time' => date('Y-m-d H:i:s')
                ]);
            }

            Db::table('b_basis_temporary')->where('id',$val['id'])->update([
                'is_del'=>1,
            ]);
        }
    }

    public function conversion($time)
    {
        $t = 24 * 60 * 60;
        $last_time = gmdate('Y-m-d H:i:s', ($time - 25569) * $t);
        return date('Y-m-d',strtotime($last_time));
    }



    // 课后辅导
    public function tutor_import($file_info,$data,$file_id){
        $title = [
            '周期开始','周期结束','班级','科目','老师id','老师','时间','类型','周期次数','周'
        ];

        $insert = [
            'admin_id' => session('admin.id'),
            'type' =>$this->type,
            'title' =>json_encode($title),
            'file_id' => $file_id,
            'create_time' => current_time()
        ];
        foreach ($data as $k=>$v){
            $name = $v[2];
            $card = $v[3];
            if(!$v[3]){
                $this->add_error($k,'无身份证');
                continue;
            }
            // 查询是否存在老师
            $admin_id = Db::table('b_admin')->where([
                'nickname'=>$name,
                'id_card'=>$card,
            ])->value('id');
            if(!$admin_id){
                $this->add_error($k,'未找到老师');
                continue;
            }
            $str = $v[4];
            $hours = 0;
            if($v[5] == '教研日'){
                $is_tdday = true;
            }else if($v[5] == '工作日'){
                $is_tdday = false;
            }else{
                $this->add_error($k,'类型错误');
                continue;
            }
            if (strpos( $str,'一') !== false || strpos( $str,'1') !== false) {
                $hours += time_week_count($file_info['start_date'],$file_info['end_date'],1,$is_tdday);
                $week = 1;
            }else if (strpos($str,'二') !== false || strpos( $str,'2') !== false) {
                $hours += time_week_count($file_info['start_date'],$file_info['end_date'],2,$is_tdday);
                $week = 2;
            }else if (strpos( $str,'三') !== false || strpos( $str,'3') !== false) {
                $hours += time_week_count($file_info['start_date'],$file_info['end_date'],3,$is_tdday);
                $week = 3;
            }else if (strpos($str,'四') !== false || strpos( $str,'4') !== false) {
                $hours += time_week_count($file_info['start_date'],$file_info['end_date'],4,$is_tdday);
                $week = 4;
            }else if (strpos( $str,'五') !== false || strpos( $str,'5') !== false) {
                $hours += time_week_count($file_info['start_date'],$file_info['end_date'],5,$is_tdday);
                $week = 5;
            }else if (strpos( $str,'六') !== false || strpos( $str,'6') !== false) {
                $hours += time_week_count($file_info['start_date'],$file_info['end_date'],6,$is_tdday);
                $week = 6;
            }else if (strpos( $str,'日') !== false || strpos( $str,'天') !== false) {
                $hours += time_week_count($file_info['start_date'],$file_info['end_date'],0,$is_tdday);
                $week = 0;
            }else{
                $this->add_error($k,'时间错误');
                continue;
            }
            $insert['data'] = json_encode([
                $file_info['start_date'],
                $file_info['end_date'],
                $v[0],
                $v[1],
                $admin_id,
                $v[2],
                $v[4],
                $v[5],
                $hours,
                $week
            ]);
            if(Db::table('b_basis_temporary')->insert($insert)){
                $this->success_count++;
            }else{
                $this->add_error($k,'临时数据入库失败');
            }
        }
        return $title;
    }

    public function tutor_implement($list){
        $b_id = 0;
        foreach ($list as $val){
            $v = json_decode($val['data'],1);
            if(!$b_id){ // 没有基准 创建基准
                $b_id = Db::table('b_benchmark_tutor')
                    ->where('start_time',$v[0])
                    ->where('end_time',$v[1])
                    ->value('id');
                if(!$b_id){
                    $b_id = Db::table('b_benchmark_tutor')
                        ->insert([
                            'admin_id' => session('admin.id'),
                            'create_time'=> date('Y-m-d H:i:s'),
                            'start_time' => $v[0],
                            'end_time' => $v[1]
                        ],'',true);
                }

            }
            if(Db::table('b_benchmark_tutor_item')->where([
                'subject'=>$v[3],
                'teacher_id'=>$v[4],
                'b_id' => $b_id,
                'type'=>$v[7],
                'week'=>$v[9],
            ])->find()){
                Db::table('b_benchmark_tutor_item')->where([
                    'subject'=>$v[3],
                    'teacher_id'=>$v[4],
                    'b_id' => $b_id,
                    'type'=>$v[7],
                    'week'=>$v[9],
                ])->update([
                    'class_name'=>$v[2],
                    'hours'=>$v[8],
                    'type'=>$v[7],
                ]);
            }else{
                Db::table('b_benchmark_tutor_item')->insert([
                    'class_name'=>$v[2],
                    'subject'=>$v[3],
                    'teacher_id'=>$v[4],
                    'week_str'=>$v[6],
                    'hours'=>$v[8],
                    'type'=>$v[7],
                    'week'=>$v[9],
                    'b_id' => $b_id,
                    'create_time' => date('Y-m-d H:i:s')
                ]);
            }
            Db::table('b_basis_temporary')->where('id',$val['id'])->update([
                'is_del'=>1,
            ]);
        }
    }
}